package com.wgchao.amc.mapstruct.mapper.expand;

import com.wgchao.amc.entity.base.*;
import com.wgchao.amc.mapstruct.DTOMapper;
import com.wgchao.amc.mapstruct.dto.SysBaseGroupDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseRoleDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseUrDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseUserDTO;
import com.wgchao.amc.mapstruct.mapper.SysBaseAgencyMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseRoleMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseUserMapper;
import com.wgchao.amc.repository.base.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @ClassName: SysBaseUserMapperExpand
 * @author: wgchao
 * @createTime: 2019/10/20 12:52 PM
 * @Description: TODO
 */
@Component
public class SysBaseUserMapperExpand implements DTOMapper<SysBaseUser, SysBaseUserDTO>, CommonExpand<SysBaseUserDTO> {
    @Autowired
    private SysBaseUserMapper userMapper;
    @Autowired
    private SysBaseUserRepository userRepository;
    @Autowired
    private SysBaseAgencyRepository agencyRepository;
    @Autowired
    private SysBaseAgencyMapper agencyMapper;
    @Autowired
    private SysBaseUgRepository ugRepository;
    @Autowired
    private SysBaseGroupRepository groupRepository;
    @Autowired
    private SysBaseGroupMapperExpand groupMapperExpand;
    @Autowired
    private SysBaseUrRepository urRepository;
    @Autowired
    private SysBaseUrMapperExpand urMapperExpand;

    @Override
    public SysBaseUser toEntity(SysBaseUserDTO userDTO) {
        return userMapper.toEntity(userDTO);
    }

    @Override
    public SysBaseUserDTO toDTO(SysBaseUser sysBaseUser) {
        return userMapper.toDTO(sysBaseUser);
    }

    @Override
    public List<SysBaseUser> toEntity(List<SysBaseUserDTO> sysBaseUserDTOS) {
        return userMapper.toEntity(sysBaseUserDTOS);
    }

    @Override
    public List<SysBaseUserDTO> toDTO(List<SysBaseUser> sysBaseUsers) {
        return userMapper.toDTO(sysBaseUsers);
    }

    @Override
    public SysBaseUserDTO getCURDUserInfo(SysBaseUserDTO t) {
        if(t.getCreateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(t.getCreateUser());
            if(userOptional.isPresent()){
                t.setCreateUserInfo(toDTO(userOptional.get()));
            }
        }
        if(t.getLastUpdateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(t.getLastUpdateUser());
            if(userOptional.isPresent()){
                t.setLastUpdateUserInfo(toDTO(userOptional.get()));
            }
        }
        if(t.getDelUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(t.getDelUser());
            if(userOptional.isPresent()){
                t.setDelUserInfo(toDTO(userOptional.get()));
            }
        }
        return t;
    }

    public SysBaseUserDTO getAgencyInfo(SysBaseUserDTO userDTO){
        Optional<SysBaseAgency> agencyOptional = agencyRepository.findById(userDTO.getAgencyCode());
        if(agencyOptional.isPresent()){
            userDTO.setAgencyInfo(agencyMapper.toDTO(agencyOptional.get()));
        }
        return userDTO;
    }

    public SysBaseUserDTO getGroupInfos(SysBaseUserDTO userDTO) {
        List<SysBaseUg> ugs = ugRepository.findAllByUserIdAndDelIsFalse(userDTO.getId());
        if(ugs!=null && !ugs.isEmpty()){
            List<SysBaseGroupDTO> groupDTOS = new ArrayList<>();
            for(SysBaseUg ug : ugs){
                Optional<SysBaseGroup> groupOptional = groupRepository.findById(ug.getGroupId());
                if(groupOptional.isPresent()){
                    groupDTOS.add(groupMapperExpand.toDTO(groupOptional.get()));
                }
            }
            userDTO.setGroupInfos(groupDTOS);
        }
        return userDTO;
    }

    public SysBaseUserDTO getRoleInfos(SysBaseUserDTO userDTO){
        List<SysBaseUr> urList = urRepository.findAllByUserIdAndDelIsFalse(userDTO.getId());
        if(urList!=null && !urList.isEmpty()){
            List<SysBaseUrDTO> roleDTOS = urList.stream().map(sysBaseUr -> {
                SysBaseUrDTO urDTO = urMapperExpand.toDTO(sysBaseUr);
                urDTO = urMapperExpand.getDetail(urDTO);
                return urDTO;
            }).collect(Collectors.toList());

            userDTO.setRoleInfos(roleDTOS);
        }
        return userDTO;
    }
}
